<!--
 * @Author: your name
 * @Date: 2021-06-24 15:57:15
 * @LastEditTime: 2021-06-27 15:56:36
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \briup-ui\src\pages\admin\info\category\List.vue
-->
<template>
  <!-- 资讯分类管理 -->
  <div>
    <div>
      <el-button class="btn" type="primary" icon="el-icon-plus" @click="toAdd" />
    </div>
    <el-table :data="tableData" style="width: 100%" :v-loading="loading">
      <el-table-column type="index" :index="1" label="序号" align="center" />
      <el-table-column prop="name" label="名称" align="center" />
      <el-table-column prop="no" label="排序号" align="center" />
      <el-table-column prop="description" label="描述" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" size="small" @click="toDelete(scope.row.id)" />
          <el-button type="primary" icon="el-icon-edit" size="small" @click="toEdit(scope.row)" />
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <el-form ref="ruleForm" :model="form" :rules="rules">
        <el-form-item label="栏目名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="排序号" :label-width="formLabelWidth">
          <el-input v-model="form.no" autocomplete="off" />
        </el-form-item>
        <el-form-item label="栏目描述" :label-width="formLabelWidth">
          <el-input v-model="form.description" type="textarea" :rows="2" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-close" @click="visible=false" />
        <el-button type="primary" icon="el-icon-check" @click="toSave" />
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { findAll } from '@/api/infoCategory'
import { saveOrUpdate, deleteById } from '@/api/article'

export default {
  data() {
    return {
      tableData: [],
      loading: false,
      title: '',
      visible: false,
      form: {},
      formLabelWidth: '80px',
      rules: {
        name: [{ required: true, message: '请输入栏目名称', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    this.findAllData()
  },
  mounted() { },
  methods: {
    async findAllData() {
      this.loading = true
      const res = await findAll()
      this.tableData = res.data
    },
    toAdd() {
      this.findAllData()
      this.form = {}
      this.title = '添加栏目信息'
      this.visible = true
    },
    toSave() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          console.log('校验通过！')
          const res = await saveOrUpdate(this.form)
          this.$notify.success({
            title: '成功',
            message: res.message
          })
          this.findAllData()
          this.visible = false
        } else {
          return false
        }
      }
      )
    },
    toClose() {
      this.$refs.ruleForm.resetFields()
    },
    async toDelete(id) {
      const res = await deleteById({ id })
      this.$notify.success({
        title: '成功',
        message: res.message
      })
      this.findAllData()
    },
    toEdit(row) {
      this.findAllData()
      this.form = { ...row }
      this.title = '编辑产品信息'
      this.visible = true
    }
  }
}
</script>
<style scoped>
</style>
